package com.handong.handserialprogram.module.base;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.utils.JLog;

/**
 * Created by userKey on 2017/12/19.
 */

public abstract class BackStackFragment<P extends BasePresenter> extends BaseFragment<P> {

    private TextView mTitleShow;

    @Override
    protected P createPresenter() {
        return null;
    }

    protected void initHeaderLayout(int resID) {
        initHeaderLayout(resID, null);
    }

    protected void initHeaderLayout(int resID, String option) {
        String title = getResources().getString(resID);
        initHeaderLayout(title, option);
    }

    protected void initHeaderLayout(String title, String option){
        FrameLayout homeLayout = (FrameLayout) findViewById(R.id.headerBackLayout);
        TextView centerTitle = (TextView) findViewById(R.id.headerCenterTitle);
        ImageView homeIcon = (ImageView) findViewById(R.id.headerBackLayoutIcon);
        homeIcon.setImageResource(R.drawable.ic_menu_back_144);
        centerTitle.setText(title);
        homeLayout.setOnClickListener(this);
        if (null != option) {
            FrameLayout optionLayout = (FrameLayout) findViewById(R.id.headerOptionLayout);
            TextView optionItem = (TextView) findViewById(R.id.headerOptionItem);
            optionLayout.setVisibility(View.VISIBLE);
            optionItem.setText(option);
            optionLayout.setOnClickListener(this);
        }
        mTitleShow = centerTitle;
    }

    public void changeTitle(String title){
        mTitleShow.setText(title);
    }

    @Override
    protected void initViewByID(View view) {
    }

    @Override
    protected void initViewData() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.headerBackLayout:
                if (!onBackClicked()) {
                    removeFrag(this);
                }
                break;
        }
    }

    protected void addFrag(BackStackFragment fragment, int containerID) {
        BackStackActivity activity = (BackStackActivity) getActivity();
        activity.addFragment(fragment, containerID);
    }

    protected void addFrag(BackStackFragment fragment, int containerID, int enterAnim, int outAnim) {
        BackStackActivity activity = (BackStackActivity) getActivity();
        activity.addFragment(fragment, containerID, enterAnim, outAnim);
    }

    protected void removeFrag(BackStackFragment fragment) {
        BackStackActivity activity = (BackStackActivity) getActivity();
        activity.removeFragment(fragment);
    }

    protected void removeFrag(BackStackFragment fragment,int enterAnim, int outAnim) {
        BackStackActivity activity = (BackStackActivity) getActivity();
        activity.removeFragment(fragment, enterAnim, outAnim);
    }

    protected boolean onBackClicked() {
        return false;
    }

    @Override
    public void onDestroyView() {
        mTitleShow = null;
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        JLog.d(this.getClass().getSimpleName(), "onDetach");
    }
}
